package com.meitutest.autoscript.camera;

import android.support.test.uiautomator.By;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.support.test.uiautomator.UiSelector;

import com.meitutest.autoscript.DeviceUtils;
import com.meitutest.autoscript.MyWatcher;
import com.meitutest.autoscript.utils.TestReport;

import java.util.Random;

/**
 * Created by CrystalChen on 2017/12/15.
 */

public class MT_CameraUtils {
    final String pkg = "com.mlab.cam";
    final String act = "com.android.camera.CameraLauncher";
    static Random random = new Random();

    UiDevice uiDevice;
    DeviceUtils deviceUtils;

    public MT_CameraUtils(UiDevice uiDevice) {
        this.uiDevice = uiDevice;
        this.deviceUtils = new DeviceUtils(uiDevice);
        MyWatcher watcher = new MyWatcher(uiDevice);
        uiDevice.registerWatcher("mywatcher", watcher);
    }

    public void openCamera() throws Exception {
        String cmd = "am start -n " + pkg + "/" + act;
        uiDevice.executeShellCommand(cmd);
        jumpGuider();
        TestReport.i("MT_CameraUtils_openCamera:  打开相机，" + pkg);
        Thread.sleep(1000);
        switchToCamera();
        Thread.sleep(1000);

    }

    public void switchToRecord() {
        if (isCameraMode()) {
            UiObject uo = uiDevice.findObject(new UiSelector().resourceId("com.mlab.cam:id/main_mode_selector"));
            try {
                uo.swipeLeft(30);
            } catch (UiObjectNotFoundException e) {
                e.printStackTrace();
            }
            if (uiDevice.hasObject(By.text("知道啦"))) {
                uiDevice.findObject(By.text("知道啦")).click();
            }
        }
    }

    public void switchToCamera() {
        if (!isCameraMode()) {
            UiObject uo = uiDevice.findObject(new UiSelector().resourceId("com.mlab.cam:id/main_mode_selector"));
            try {
                uo.swipeRight(30);
            } catch (UiObjectNotFoundException e) {
                e.printStackTrace();
            }
        }
    }

    public void openVedio() throws Exception {
        String cmd = "am start -n " + pkg + "/" + act;
        uiDevice.executeShellCommand(cmd);
        jumpGuider();
        TestReport.i("MT_CameraUtils_openCamera: 打开相机，切到视频模式" + pkg);
        Thread.sleep(1000);
        switchToRecord();
        Thread.sleep(1000);

    }

    public void focusStressTest(int count) {
        try {
            openCamera();
            for (int i = 0; i < count; i++) {
                int x = random.nextInt(uiDevice.getDisplayWidth());
                int y = random.nextInt(uiDevice.getDisplayHeight() - 700) + 179;
                uiDevice.click(x, y);
                TestReport.i("MT_CameraUtils_randomClick: 第" + i + "次随机点击（" + x + "," + y + "）");
            }
            exitCamera();

        } catch (Exception e) {
            e.printStackTrace();
            TestReport.e("MT_CameraUtils_focusStressTest: " + e.getMessage());
        }
    }

    private void jumpGuider() throws Exception {
        Thread.sleep(1000);
        if (uiDevice.hasObject(By.text("同意"))) {
            uiDevice.findObject(By.text("同意")).click();
            Thread.sleep(500);
        }
        if (uiDevice.hasObject(By.text("知道了"))) {
            uiDevice.findObject(By.text("知道了")).click();
            Thread.sleep(500);
        }
        UiObject ui = uiDevice.findObject(new UiSelector().resourceId("com.mlab.cam:id/guide_content"));
        if (!ui.exists()) {
            TestReport.i("MT_CameraUtils_jumpGuider: 当前没有引导页");
            return;
        }
        for (int i = 0; i < 4; i++) {
            ui.swipeLeft(20);
            Thread.sleep(200);
        }

        UiObject f = uiDevice.findObject(new UiSelector().resourceId("com.mlab.cam:id/guide_finished"));
        f.click();
        Thread.sleep(2000);

    }

    public void exitCamera() {
        while (deviceUtils.isInPkg(pkg)) {
            uiDevice.pressBack();
        }
        uiDevice.removeWatcher("mywathcer");
    }

    public void takePhotoStressTest(long period, long duration) {
        try {
            openCamera();
            long start = System.currentTimeMillis();
            while (System.currentTimeMillis() - start < duration) {
                Thread.sleep(period);
                UiObject shutter = uiDevice.findObject(new UiSelector().resourceId("com.mlab.cam:id/shutter_button"));
                shutter.click();
                TestReport.i("MT_CameraUtils_takePhotoStressTest: 拍照");
            }
            exitCamera();
        } catch (Exception e) {
            e.printStackTrace();
            TestReport.e("MT_CameraUtils_takePhotoStressTest: " + e.getMessage());
        }
    }

    public void vedioStressTest(long duration) {
        try {
            openVedio();
            long start = System.currentTimeMillis();
            while (System.currentTimeMillis() - start < duration) {
                UiObject shutter = uiDevice.findObject(new UiSelector().resourceId("com.mlab.cam:id/shutter_button"));
                shutter.click();
                Thread.sleep(duration);
                shutter.click();
                TestReport.i("MT_CameraUtils_takePhotoStressTest: 录像");
            }
            exitCamera();
        } catch (Exception e) {
            e.printStackTrace();
            TestReport.e("MT_CameraUtilss_takePhotoStressTest: " + e.getMessage());
        }
    }

    public boolean isCameraMode() {
        UiObject auto = uiDevice.findObject(new UiSelector().text("美颜"));
        try {
            return auto.isClickable();
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
            return false;
        }

    }


}
